<?php
function getLanguages($renderType='select') {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_sysLanguages ORDER BY LanguageName";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	while ($value = mysql_fetch_assoc($result)) {
		$langID = $value[LanguageID];
		$lang   = stripslashes($value[LanguageName]);
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$lang.'"';
			if ($lang == 'English') {
				$returnString .= ' selected';
			}
			$returnString .= '>'.$lang.'</option>';			
		} elseif ($renderType == 'json') {
			
		}
	}	
	return $returnString;
}

function getNationalities($renderType='select') {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_sysNationalities ORDER BY NationalityName";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	while ($value = mysql_fetch_assoc($result)) {
		$natlID = $value[NationalityID];
		$natl   = stripslashes($value[NationalityName]);
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$natl.'">'.$natl.'</option>';			
		} elseif ($renderType == 'json') {
			
		}
	}	
	return $returnString;
}

function getEthnicities($renderType='select') {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_sysEthnicity ORDER BY EthnicityName";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	while ($value = mysql_fetch_assoc($result)) {
		$ethID = $value[EthnicityID];
		$eth   = stripslashes($value[EthnicityName]);
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$eth.'">'.$eth.'</option>';			
		} elseif ($renderType == 'json') {
			
		}
	}	
	return $returnString;
}

function getRaces($renderType='select') {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_sysRacialGroups ORDER BY RacialGroupName";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	while ($value = mysql_fetch_assoc($result)) {
		$raceID = $value[RacialGroupID];
		$race   = stripslashes($value[RacialGroupName]);
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$race.'">'.$race.'</option>';			
		} elseif ($renderType == 'json') {
			
		}
	}	
	return $returnString;
}

function getDistricts($renderType='select') {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_sysSchoolDistricts ORDER BY SchoolDistrictName";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	$stringJSON   = '({"Districts":[';
	while ($value = mysql_fetch_assoc($result)) {
		$sdID = $value[SDID];
		$sd   = trim(stripslashes($value[SchoolDistrictName]));
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$sdID.'">'.$sd.'</option>'."\n";			
		} elseif ($renderType == 'json') {
			$newArray = array(
							'DistID'   => $sdID,		
							'District' => $sd
							);
			$stringJSON .= Zend_Json::encode($newArray).',';
		}
	}	
	if ($renderType == 'json') {
		$stringJSON = substr($stringJSON,0,-1);
		$stringJSON .= ']})';
		$returnString = $stringJSON;
	}
	return $returnString;
}

function getSchoolsPrograms() {
	
}

function getIEPClassifications($renderType='select') {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_sysDisabilities 
			WHERE
				PHC = 1
	    	ORDER BY 
				PHCNumber";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	$stringJSON   = '({"PHCCodes":[';
	while ($value = mysql_fetch_assoc($result)) {
		$dID      = $value[DisabilityID];
		$dName    = stripslashes($value[DisabilityName]);
		$dPHCNum  = $value[PHCNumber];
		$dPHCCode = $value[PHCCode];
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$dID.'">'.$dPHCNum.' '.$dPHCCode.' '.$dName.'</option>'."\n";			
		} elseif ($renderType == 'json') {
			$newArray = array(
							
							);
			$stringJSON .= Zend_Json::encode($newArray).',';
		}
	}	
	if ($renderType == 'json') {
		$stringJSON = substr($stringJSON,0,-1);
		$stringJSON .= ']})';
		$returnString = $stringJSON;
	}
	return $returnString;
}

function getSPEDDesignations() {
	
}

function getDiagnoses($renderType='select') {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_Diagnosis ORDER BY DiagnosisName";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	$stringJSON   = '({"Diagnoses":[';
	
	while ($value = mysql_fetch_assoc($result)) {
		$diagID   = $value[DiagnosisID];
		$diagName = stripslashes($value[DiagnosisName]);
		$diagDef  = stripslashes($value[Definition]);
		
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$sdid.'">'.$sd.'</option>';			
		} elseif ($renderType == 'json') {
			$newArray = array(
							'DiagID'    => $diagID,		
							'Diagnosis' => $diagName,
							'Definition'=> $diagDef
							);
			$stringJSON .= Zend_Json::encode($newArray).',';
		}
	}	
	if ($renderType == 'json') {
		$stringJSON = substr($stringJSON,0,-1);
		$stringJSON .= ']})';
		$returnString = $stringJSON;
	}
	return $returnString;
}

function getRoles($memberType=null,$renderType='select',$org=null) {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_sysRoles 
			WHERE 
				MemberType = '$memberType' AND 
				OrgFlag    = '$org' AND
				Active = 1
			ORDER BY Role";
	$result = mysql_db_query($dbname,$sql);
	   
	$returnString = '';
	$stringJSON   = '({"Roles":[';
	
	while ($value = mysql_fetch_assoc($result)) {
		$roleID   = $value[RoleID];
		$roleName = stripslashes($value[Role]);
		
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$roleID.'">'.$roleName.'</option>';			
		} elseif ($renderType == 'json') {
			$newArray = array(
							'RoleID' => $roleID,		
							'Role'   => $roleName
							);
			$stringJSON .= Zend_Json::encode($newArray).',';
		}
	}	
	if ($renderType == 'json') {
		$stringJSON = substr($stringJSON,0,-1);
		$stringJSON .= ']})';
		$returnString = $stringJSON;
	}
	return $returnString;
}

function getDisabilities($renderType='select') {
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_sysDisabilities ORDER BY DisabilityName";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	while ($value = mysql_fetch_assoc($result)) {
		$disID = $value[DisabilityID];
		$dis   = stripslashes($value[DisabilityName]);
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$disID.'">'.$dis.'</option>';			
		} elseif ($renderType == 'json') {
			
		}
	}	
	return $returnString;
}

function getOrganizations() {
	
}

function getStaff($renderType='select') {
	//global $dbConn;
	global $dbname, $db, $db_selected, $pre;
		
	$sql = "SELECT * from cdc_Users
			WHERE 
			Organization = '".$_SESSION['org']."' AND
			Active = 1
			ORDER BY StaffLName";
	$result = mysql_db_query($dbname,$sql);
	//$result = $dbConn->fetchAll($sql);
	   
	$returnString = '';
	$stringJSON   = '({"Staff":[';
	
	$a=0;
	while ($value = mysql_fetch_assoc($result)) {
		$staffID       = $value[StaffID];
		$nameFirst     = stripslashes($value[StaffFName]);
		$nameLast      = stripslashes($value[StaffLName]);
		$accessLevel   = $value[LevelSecurity];
		$staffUserName = $value[UserName];
		$staffPassword = $value[Password];
		$staffAddress  = stripslashes($value[StaffAddress]); 
		$staffCity     = stripslashes($value[StaffCity]);
		$staffState    = $value[StaffState];
		$staffZip      = $value[StaffZip];
		$staffEmail    = $value[StaffEmail];
		$staffPhone    = $value[StaffPhone];
		$dateUpdate    = $value[DateUpdate];
		$entryUserid   = $value[Userid];
					
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$staffID.'">'.$nameFirst.' '.$nameLast.'</option>';			
		} elseif ( ($renderType == 'json') || ($renderType == 'array') )  {
			$newArray = array(
							'StaffID'    => $staffID,		
							'NameFirst'  => $nameFirst,
							'NameLast'   => $nameLast,
							'AccessLevel' => $accessLevel,
							'UserName'    => $staffUserName,
							'Password'    => $staffPassword,
							'Address'     => $staffAddress,
							'City'        => $staffCity,
							'State'       => $staffState,
							'Zip'         => $staffZip,
							'Email'       => $staffEmail,
							'Phone'       => $staffPhone,
							'DateUpdate'  => $dateUpdate,
							'EntryUserid' => $entryUserid
							);
			$stringJSON .= Zend_Json::encode($newArray).',';
			$returnArray[$a] = $newArray;
		} 
		$a++;
	}	
	if ($renderType == 'json') {
		$stringJSON = substr($stringJSON,0,-1);
		$stringJSON .= ']})';
		$returnString = $stringJSON;
	} elseif ($renderType == 'array') {
		$returnString = $returnArray;
	}
	return $returnString;
}

function getStaffInfo($staffID) {
	global $dbname, $db, $db_selected;
	
	$sql = "SELECT * from cdc_Users WHERE StaffID = '$staffID'";
	$result = mysql_db_query($dbname,$sql);
	$staffArray = mysql_fetch_assoc($result);
	
	if (empty($staffArray['StaffPhone'])) {
		$staffArray['StaffPhone'] = '[none]';
	}
	
	return $staffArray;
}

function getDiagnosisID($diagnosisName) {
	global $dbname, $db, $db_selected, $pre;
	
	$sql = "SELECT DiagnosisID from cdc_Diagnosis WHERE DiagnosisName = '$diagnosisName'";
	$result = mysql_db_query($dbname,$sql);
	$diagnosisID = mysql_fetch_assoc($result);
	$diagnosisID = $diagnosisID['DiagnosisID'];

	return $diagnosisID;
}

function getDiagnosisName($diagnosisID) {
	global $dbname, $db, $db_selected, $pre;
	
	$sql = "SELECT DiagnosisName from cdc_Diagnosis WHERE DiagnosisID = '$diagnosisID'";
	$result = mysql_db_query($dbname,$sql);
	$diagnosisName = mysql_fetch_assoc($result);
	$diagnosisName = $diagnosisName['DiagnosisName'];
	return $diagnosisName;
}

function getNextFamilyID() {
	global $dbname, $db, $db_selected;
	
	$sqlGetFamilyID = "SELECT MAX(FamilyID) AS LastFamilyID from cdc_linkMemberFamily";
	$resultGetFamilyID = mysql_db_query($dbname,$sqlGetFamilyID);
	$resultArray = mysql_fetch_assoc($resultGetFamilyID);
	$familyID = $resultArray['LastFamilyID'];
	$familyID = $familyID+1;	
	
	return $familyID;
}

function getCategories($siteArea,$renderType='select',$catType='main',$catID=null,$selectedCat=null) {
	global $dbname, $db, $db_selected;
	
	$siteArea = urldecode($siteArea);
	
	if ($catType == 'sub') {
		$subCatSQL = ' ParentID = '.$catID.' AND ';
	} else {
		$subCatSQL = ' ParentID = 0 AND ';
	}
	
	if ( empty($siteArea) || ($siteArea == 'null') ) {
		/*
		 *  We better be looking for sub categories based on a mainCatID
		 */
		$siteAreaSQL = '';
	} else {
		$siteAreaSQL = " SiteArea = '$siteArea' AND ";
	}
	
	$sql = "SELECT * from cdc_sysCategory 
			WHERE 
			$siteAreaSQL
			$subCatSQL
			Active = 1
			ORDER BY DisplayOrder,CatTitle";
	$result = mysql_db_query($dbname,$sql);
	if (mysql_num_rows($result)>0) {
		$returnString = '';
		$stringJSON   = '({"CatType":"'.$catType.'","Categories":[';
		$stringTreeJSON = '[';
		
		while ($value = mysql_fetch_assoc($result)) {
			$catID    = $value['CatID'];
			$catTitle = stripslashes($value['CatTitle']);
			
			if ($renderType == 'select') {
				$returnString .= '<option value="'.$catID.'"';
				if ($selectedCat == $catID) {
					$returnString .= ' selected ';
				}
				$returnString .= '>'.$catTitle.'</option>';			
			} elseif ($renderType == 'json') {
				$newArray = array(
								'CatID'    => $catID,		
								'CatTitle' => $catTitle
								);
				$stringJSON .= Zend_Json::encode($newArray).',';
			} elseif ($renderType == 'tree') {
				$stringTreeJSON .= '{"text":"'.$catTitle.'","id":"'.$catID.'","level":2,"cls":"folder"},';
			}
		}	
		if ($renderType == 'json') {
			$stringJSON = substr($stringJSON,0,-1);
			$stringJSON .= ']})';
			$returnString = $stringJSON;
		} elseif ($renderType == 'tree') {
			$stringTreeJSON = substr($stringTreeJSON,0,-1);
			$stringTreeJSON .= ']';
			$returnString = $stringTreeJSON;
		}
	} else {
		$returnString = 0;	
	}
	return $returnString;	
}

function getLinks($siteArea=null,$renderType='select') {
	global $dbname, $db, $db_selected;
	
	if (!empty($siteAreay)) {
		$where = 'WHERE '.$siteArea.' = 1 ';
	}
	
	$sql = "SELECT * from cdc_Links $where";
	$result = mysql_db_query($dbname,$sql);
	
	$returnString = '';
	$stringJSON   = '({"Links":[';
	
	while ($value = mysql_fetch_assoc($result)) {
		$linkID   = $value['URLID'];
		$linkURL  = $value['URL'];
		$linkDesc = stripslashes($value['URLDescription']);
		
		if ($renderType == 'select') {
			$returnString .= '<option value="'.$linkID.'">'.$linkURL.'</option>';			
		} elseif ($renderType == 'json') {
			$newArray = array(
							'LinkID'  => $linkID,		
							'LinkURL' => $linkURL,
							'LinkDesc'=> $linkDesc
							);
			$stringJSON .= Zend_Json::encode($newArray).',';
		}
	}	
	if ($renderType == 'json') {
		$stringJSON = substr($stringJSON,0,-1);
		$stringJSON .= ']})';
		$returnString = $stringJSON;
	}
	return $returnString;	
}

function getStatusInfo($itemID,$statusType) {
	global $dbname, $db, $db_selected;
	
	if ($statusType == 'contact') {
		$sql = "SELECT DateofContact,DateofClosure,Status,DateUpdate
				FROM cdc_Contacts WHERE ContactID = '$itemID'";
		$result = mysql_db_query($dbname,$sql);
		$row = mysql_fetch_assoc($result);
		
		$dateContact = $row['DateofContact'];
		$dateClose   = $row['DateofClosure'];
		$dateUpdate  = $row['DateUpdate'];
		$statusCode  = $row['Status'];
			
		/*
		 * See if contact is overdue for closure
		 */
		$statusClassRow  = '';
		
		if ($statusCode != 3) {
			$today = date('Y-m-d');
			$maxDays = CONTACT_CLOSURE_WINDOW;
			
			if (daysBetweenDates($dateContact,$today)>$maxDays) {
				/*
				 * We're late closing this contact
				 */
				$overdue = 1;
				$statusClassBox  = 'statusAlertRow';
			}
		}
		
		switch ($statusCode) {
			case 1:
				$statusClassIcon = 'statusOpen';
				$statusClassBox  = 'statusOpenBox';
				$statusText      = 'In progress';
				break;
			case 2:
				$statusClassIcon = 'statusTransferred';
				$statusClassBox  = 'statusOpenBox';
				$statusText  = 'Sent for follow-up';
				break;
			case 3:
				$statusClassIcon = 'statusClosed';
				$statusClassBox  = 'statusClosedBox';
				$statusText  = 'Closed';
				break;	
			case 4:
				$statusClassIcon = 'statusReopen';
				$statusClassBox  = 'statusOpenBox';
				$statusText      = 'Reopened';
				break;	
			case 5:
				$statusClassIcon = 'statusAlert';
				$statusClassBox  = 'statusAlertRow';
				$statusText  = 'Closure Overdue!';
				break;			
		}
	}
	$statusArray = array($statusClassIcon,$statusText,$statusClassBox,$dateClose);
	return $statusArray;
}

function getDirFromCategory($catID) {
	global $dbname, $db, $db_selected;
	$returnArray = array();
	
	$sql2 = "SELECT ParentID,SiteArea,DirectoryName from cdc_sysCategory WHERE CatID = '".$catID."'";
	$result2 = mysql_db_query($dbname,$sql2);
	$row2 = mysql_fetch_assoc($result2);
	if ($row2['ParentID'] == 0) {
		$returnArray['MainCatID']  = $catID;	
		$returnArray['MainCatDir'] = $row2['DirectoryName'];
		$returnArray['CatPath']    = $returnArray['MainCatDir']; 		
	} else {
		$returnArray['MainCatID'] = $row2['ParentID'];
		$returnArray['SubCatID']  = $catID;
		
		$sql3 = "SELECT DirectoryName from cdc_sysCategory WHERE CatID = '".$returnArray['MainCatID']."'";
		$result3 = mysql_db_query($dbname,$sql3);
		$row3 = mysql_fetch_assoc($result3);
		
		$returnArray['MainCatDir'] = $row3['DirectoryName'];
		$returnArray['SubCatDir']  = $row2['DirectoryName'];
		$returnArray['CatPath']  = $returnArray['MainCatDir'].'/'.$returnArray['SubCatDir'];
	}
	$returnArray['SiteArea'] = $row2['SiteArea']; 
	return $returnArray;
}

function getTags($siteArea,$renderType='html',$selectedTag=null) {
	/*
	 * We can have 3 render types with tags:
	 * 1. Plain ol' HTML (like del.icio.us
	 * 2. Select
	 * 3. JSON
	 */
	global $dbname, $db, $db_selected;
	
	$sql = "SELECT TagName from cdc_sysTags 
			WHERE SiteArea = '$siteArea'
			ORDER BY TagName";
	$result = mysql_db_query($dbname,$sql);
	
	if (mysql_num_rows($result)>0) {
		$returnString = '';
		$stringJSON   = '({"Tags":[';
		$stringHTML   = '<div id="tagsList">';
		
		while ($value = mysql_fetch_assoc($result)) {
			$tagID   = $value['TagID'];
			$tagName = stripslashes($value['TagName']);
			
			if ($renderType == 'select') {
				$returnString .= '<option value="'.$tagID.'"';
				if ($selectedTag == $tagID) {
					$returnString .= ' selected ';
				}
				$returnString .= '>'.$tagName.'</option>';			
			} elseif ($renderType == 'json') {
				$newArray = array(
								'TagID'   => $tagID,		
								'TagName' => $tagName
								);
				$stringJSON .= Zend_Json::encode($newArray).',';
			} elseif ($renderType == 'html') {
				$stringHTML .= '<span>'.$tagName.'</span> &nbsp;';
			}
		}	
		if ($renderType == 'json') {
			$stringJSON = substr($stringJSON,0,-1);
			$stringJSON .= ']})';
			$returnString = $stringJSON;
		} elseif ($renderType == 'html') {
			$stringHTML .= '</div>';
			$returnString = $stringHTML;
		}
	} else {
		$returnString = 0;	
	}
	return $returnString;	
	
}

function saveNewTags($siteArea,$tagString) {
	global $dbname, $db, $db_selected;
	
	$tagArray = explode(' ',$tagString);
	foreach($tagArray as $tag) {
		$tag = trim($tag);
		if (!empty($tag)) {		
			$sql = "SELECT * from cdc_sysTags
					WHERE
					TagName  = '$tag' AND
					SiteArea = '$siteArea'";
			$result = mysql_db_query($dbname,$sql);
			
			if (mysql_num_rows($result)<1) {
				$sql = "INSERT into cdc_sysTags (
						TagName,
						SiteArea,
						DateUpdate,
						Userid
						) values (
						'$tag',
						'$siteArea',
						'".date('Y-m-d')."',
						'".$_SESSION['userid']."'
						)";
				$result = mysql_db_query($dbname,$sql);
			}
		}	
	}
}

function getMemberInfo($memberID) {
	global $dbname, $db, $db_selected, $pre;
	
	$sql = "SELECT * from cdc_ContactClientDemographics WHERE MemberID = '$memberID'";
	$result = mysql_db_query($dbname,$sql);
	$rowMember = mysql_fetch_assoc($result);
	
	return $rowMember;
}

?>